var _api = _interopRequireDefault(require("../../../../utils/api")), _util2 = _interopRequireDefault(require("../../../../utils/util")), _cardUtil = _interopRequireDefault(require("../../../../utils/card-util")), _util3 = _interopRequireDefault(require("../../../../public/utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var a = null != arguments[t] ? arguments[t] : {}, r = Object.keys(a);
        "function" == typeof Object.getOwnPropertySymbols && (r = r.concat(Object.getOwnPropertySymbols(a).filter(function(t) {
            return Object.getOwnPropertyDescriptor(a, t).enumerable;
        }))), r.forEach(function(t) {
            _defineProperty(e, t, a[t]);
        });
    }
    return e;
}

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, a = new Array(t.length); e < t.length; e++) a[e] = t[e];
        return a;
    }
}

var emptyTip = "查看名片时，点击收藏可放入名片夹...", appData = getApp().globalData, sortList = [ {
    text: "按时间",
    type: "addtime",
    orderBy: "desc"
}, {
    text: "按姓名",
    type: "realnameLetter",
    orderBy: "asc"
}, {
    text: "按公司",
    type: "corpnameLetter",
    orderBy: "asc"
} ], STAR_NAME = "星标好友", STAR_GUIDE_KEY = "star.guide.key", LIMIT = 30;

Component({
    properties: {
        showViewGuide: Boolean
    },
    data: {
        STAR_NAME: STAR_NAME,
        inputShow: !1,
        sortIndex: "0",
        sortList: sortList.map(function(t) {
            return "".concat(t.text, "排序");
        }),
        search: {
            keyword: "",
            loading: !1,
            result: {
                count: 0,
                list: []
            }
        },
        count: 1,
        VERSION: _cardUtil.default.VERSION,
        showSkeleton: !0
    },
    ready: function() {
        this.onLoad();
    },
    methods: {
        onLoad: function() {
            var e = this;
            this.setCardData(appData.card), this.$events = [ [ "card.change", this.setCardData.bind(this) ], [ "cardlist.update", function() {
                return e.loadList(!0);
            } ] ], this.$events.forEach(function(t) {
                return e.$addListener.apply(e, _toConsumableArray(t));
            });
        },
        onPullDownRefresh: function() {
            this.loadList(!0, wx.stopPullDownRefresh);
        },
        onReachBottom: function() {
            this.loadList();
        },
        setCardData: function(t) {
            var e = this, a = {
                emptyTip: emptyTip,
                guid: t.guid,
                showSkeleton: !0,
                groupedKeys: [ "###" ],
                groupedList: {
                    "###": Array(10).fill({
                        realname: "****",
                        corpname: "****"
                    })
                }
            };
            this.loadList(!0), this.setData(a, function() {
                return e.selectComponent("#skeleton").create(e);
            });
        },
        tiggerSearch: function(t) {
            var e = {
                inputShow: t
            };
            t || (e["search.result"] = {}, e["search.keyword"] = "", this.searchListChange && (this.searchListChange = !1, 
            this.$emit("cardlist.update"))), this.setData(e);
        },
        onSearch: function(t) {
            var e = this;
            clearTimeout(this.searchTimeout), this.searchTimeout = setTimeout(function() {
                e.setData({
                    "search.keyword": t,
                    "search.loading": !0
                }), _api.default.weappcard.search({
                    data: {
                        keyword: t
                    }
                }).then(function(t) {
                    e.setData({
                        "search.result.count": t.data.count,
                        "search.result.list": _util3.default.addThumbToList(t.data.lists),
                        "search.loading": !1
                    });
                });
            }, 500);
        },
        resetData: function() {
            this.offset = 0, this.hasMore = !0;
        },
        loadList: function(e, t) {
            var a = this;
            e && this.resetData();
            var r = this.data.sortIndex, i = sortList[r], o = i.type, n = i.orderBy;
            return _api.default.weappcard.favlistv2({
                data: {
                    offset: this.offset,
                    sortby: o,
                    orderby: n,
                    limit: LIMIT
                },
                complete: t
            }).then(function(t) {
                return a.handleLoadListResult(t, o, e);
            });
        },
        handleLoadListResult: function(t, e, a) {
            var r = t.data, i = r.list, o = r.count, n = this["groupBy".concat(_util3.default.ucFirst(e))](i);
            if ("realnameLetter" === e && _util2.default.sortByLetterKey(n.groupedKeys), this.lists = a ? i : this.lists.concat(i), 
            a) this.setData(_objectSpread({
                count: o,
                showSkeleton: !1
            }, n)); else {
                var s = _util2.default.concatListToData(n, this.data);
                this.setData(s);
            }
            this.offset = this.offset + LIMIT, this.hasMore = LIMIT <= i.length, this.checkIsShowStarGuide();
        },
        groupByRealnameLetter: function(t) {
            return t = t.map(function(t) {
                return t.realnameLetter = t.realnameLetter || "#", t;
            }), _util3.default.groupByLetterKey(t, "realnameletter");
        },
        groupByCorpnameLetter: function(t) {
            return t = t.map(function(t) {
                return t.corpname = t.corpname || "#", t;
            }), _util3.default.groupByKey(t, "corpname");
        },
        groupByAddtime: function(t) {
            return _util3.default.groupByTimestampDesc(t, "addtime");
        },
        call: function(t) {
            wx.makePhoneCall({
                phoneNumber: t.currentTarget.dataset.mobile
            });
        },
        selectSortBy: function(t) {
            var e = t.detail.value;
            this.data.sortIndex !== e && (this.setData({
                sortIndex: e
            }), this.loadList(!0));
        },
        gotoView: function(t) {
            var e = t.currentTarget.dataset.url;
            wx.navigateTo({
                url: e
            });
        },
        onCellSwipe: function(t) {
            var e, a, r, i, o = this, n = t.detail, s = n.method, u = n.index, c = n.key;
            i = c ? (a = e = this.lists, r = this.data.groupedList[c][u], "quietCardList") : (a = e = this.data.search.result.list, 
            r = e[u], "quietSearchList"), this[s].call(this, r, a).then(function() {
                o[i].call(o, e);
            });
        },
        onStar: function(t) {
            var e = this;
            return t.istop = +t.istop, _api.default.weappcard.setfavtop({
                data: {
                    cardid: t.cardid,
                    op: t.istop ? "del" : "add"
                }
            }).then(function() {
                wx.showToast({
                    title: "".concat(t.istop ? "取消" : "标星", "成功")
                }), t.istop = t.istop ? "0" : "1", e.data.showStarGuideModal && e.hideStarGuide();
            });
        },
        onFav: function(r, i) {
            return new Promise(function(e, a) {
                wx.fPromise("showModal", {
                    content: "确定取消收藏该名片?"
                }).then(function(t) {
                    if (!t.confirm) return a();
                    _api.default.weappcard.setfav({
                        data: {
                            cardid: r.cardid,
                            op: "del"
                        }
                    }).then(function() {
                        var t = i.findIndex(function(t) {
                            return r.cardid === t.cardid;
                        });
                        i.splice(t, 1), wx.showToast({
                            title: "取消成功"
                        }), e();
                    }).catch(a);
                });
            });
        },
        quietCardList: function() {
            var t = sortList[this.data.sortIndex].type, e = this["groupBy".concat(_util3.default.ucFirst(t))](this.lists);
            "realnameLetter" === t && _util2.default.sortByLetterKey(e.groupedKeys), this.setData(e);
        },
        quietSearchList: function(t) {
            this.searchListChange = !0, this.setData({
                "search.result.list": t
            });
        },
        checkIsShowStarGuide: function() {
            0 < this.data.groupedKeys.filter(function(t) {
                return t === STAR_NAME;
            }).length || (wx.getStorageSync(STAR_GUIDE_KEY) || this.setData({
                showStarGuideModal: !0
            }));
        },
        hideStarGuide: function() {
            var t = this;
            wx.setStorage({
                key: STAR_GUIDE_KEY,
                data: 1,
                success: function() {
                    return t.setData({
                        showStarGuideModal: !1
                    });
                }
            });
        },
        reportScan: function() {
            this.$report("cardcase_tap_shoot");
        }
    }
});